<template>
  <div class="login-container">
    <el-form ref="loginForm" :model="loginForm" label-width="80px" class="login-form">
      <h2 class="login-title">球员管理系统登录</h2>
      <el-form-item label="用户名：" prop="username">
        <el-input v-model="loginForm.username"></el-input>
      </el-form-item>
      <el-form-item label="密码：" prop="password">
        <el-input type="password" v-model="loginForm.password"></el-input>
      </el-form-item>
      <el-form-item label="角色：" prop="role">
        <el-radio-group v-model="loginForm.role">
          <el-radio label="admin">管理员</el-radio>
          <el-radio label="player">球员</el-radio>
          <el-radio label="coach">教练</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item class="login-button">
        <el-button :loading="loading" type="primary" @click="login">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      loginForm: {
        username: '',
        password: '',
        role: 'admin' // 默认角色
      },
      loading: false, // 添加 loading 状态
      isLoggingIn: false // 防止重复点击
    };
  },
  methods: {
    async login() {
      if (this.isLoggingIn) return; // 防止重复点击
      this.isLoggingIn = true;

      if (!this.loginForm.username || !this.loginForm.password) {
        this.$message.error('用户名和密码不能为空');
        this.isLoggingIn = false;
        return;
      }

      const roleUrlMap = {
        admin: 'http://localhost:8080/login/admin',
        player: 'http://localhost:8080/login/player',
        coach: 'http://localhost:8080/login/coach'
      };

      const url = roleUrlMap[this.loginForm.role];

      if (!url) {
        this.$message.error('请选择角色');
        this.isLoggingIn = false;
        return;
      }

      this.loading = true; // 开始加载
      try {
        console.log('Sending login request to:', url);
        const response = await axios.post(url, {
          username: this.loginForm.username,
          password: this.loginForm.password
        });

        console.log('Login response:', response);

        if (response.data.code === 1) {
          const name = response.data.data; // 提取后端返回的name
          this.$message.success('登录成功，欢迎 ' + name);
          this.$emit('loginSuccess', { username:this.loginForm.username, role: this.loginForm.role, name: name });
        } else {
          this.$message.error(response.data.msg);
        }
      } catch (error) {
        console.error('登录请求失败', error);
        this.$message.error('登录请求失败');
      } finally {
        this.loading = false; // 结束加载
        this.isLoggingIn = false;
      }
    }
  }
};
</script>


<style scoped>
.login-container {
  height: 100vh;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background-image: url('src/images/1.jpg'); /* 替换为你的背景图片路径 */
  background-size: cover;
  background-position: center;
}

.login-title {
  font-size: 24px;
  font-weight: bold;
  margin-bottom: 20px;
}

.login-form {
  width: 400px;
  background-color: #f0f2f5;
  padding: 20px;
  border-radius: 5px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.login-button {
  display: flex;
  justify-content: center;
  margin-left: 80px;
}
</style>
